package cn.hzc.fortess.core;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.HashMap;
import java.util.Map;


public class ServletCtx implements ServletContextListener {

    private final static Map<String, String> ctx = new HashMap<>();

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        ctx.clear();
    }

    @Override
    public void contextInitialized(ServletContextEvent evt) {
        ServletContext sc = evt.getServletContext();

        //项目发布,当前运行环境的绝对路径
        ctx.put("realPath", sc.getRealPath("/").replaceFirst("/", ""));

        //servletContextPath,默认""
        ctx.put("contextPath", sc.getContextPath());

    }

    public static String getContextPath(){
        return ctx.get("contextPath");
    }

}
